package com.java2007.service.impl;

import com.java2007.dao.OrderDao;
import com.java2007.dao.OrderDetailDao;
import com.java2007.entity.CartItem;
import com.java2007.entity.Result;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Admin;
import com.java2007.pojo.Order;
import com.java2007.pojo.OrderDetail;
import com.java2007.service.OrderService;
import com.java2007.util.JdbcUtil;

import java.sql.SQLException;
import java.util.*;

/**
 * 订单事务层
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao= (OrderDao) BeanFactory.getBean("com.java2007.dao.impl.OrderDaoImpl");
    private OrderDetailDao orderDetailDao= (OrderDetailDao) BeanFactory.getBean("com.java2007.dao.impl.OrderDetailDaoImpl");
    /**
     * 创建订单的方法
     * @param list
     * @param finalPrice
     * @param tableId
     * @return
     */
    @Override
    public Result save(List<CartItem> list, Long finalPrice, Integer tableId, Admin admin) {
        try {
            JdbcUtil.begin();
            Order order=new Order();
            order.setId(System.currentTimeMillis());
            order.setTotalPrice(finalPrice);
            order.setTableId(tableId);
            order.setPayStatus(0);
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            order.setCreateUser(admin.getAdminId());
            Thread.sleep(30);
            orderDao.insert(order);
            List<OrderDetail> lists=new ArrayList<>();
            for (CartItem cartItem : list) {
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setId(System.currentTimeMillis());
                orderDetail.setFoodId(cartItem.getFoodId());
                orderDetail.setFoodName(cartItem.getFoodName());
                orderDetail.setNum(cartItem.getNum());
                orderDetail.setFoodPrice(cartItem.getFoodPrice());
                orderDetail.setTotalPrice(cartItem.getTotalPrice());
                orderDetail.setOrderId(order.getId());
                orderDetail.setCreateTime(new Date());
                orderDetail.setUpdateTime(new Date());
                //储存订单详情
                Thread.sleep(30);
                orderDetailDao.insert(orderDetail);
                Thread.sleep(30);
                lists.add(orderDetail);
            }
            JdbcUtil.commit();
            //集合包装数据
            Map<String,Object> map=new HashMap<>();
            map.put("orderId",order.getId());
            map.put("total_price",order.getTotalPrice());
            map.put("orderDetailList",lists);
            return Result.ok("订单创建成功",map);
        } catch (SQLException | InterruptedException e) {
            e.printStackTrace();
            JdbcUtil.rollback();
            return Result.fail("订单创建失败");
        }
    }

    /**
     * 查询同一桌的所有订单的消费总额
     * @param tableId
     */
    @Override
    public Long getMoney(Integer tableId) throws SQLException {
        List<Order>list=orderDao.findByTableId(tableId,0);
        Long sum=0L;
        for (Order order:list) {
            sum+=order.getTotalPrice();
        }
        return sum;
    }

    /**
     * 修改同一桌的所有订单的支付状态
     * @param tableId
     */
    @Override
    public int changeStatus(Integer tableId, int i) {
        int a=0;
        try {
            JdbcUtil.begin();
            a=orderDao.updateStatus(tableId,i);
            JdbcUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtil.rollback();
        }
        return a;
    }
}
